package com.itheima.controller;


import com.itheima.feign.UserClient;
import com.itheima.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

//@RestController
//@RequestMapping("consumer")
//public class ConsumerController {
 //   @Autowired
 //    private RestTemplate restTemplate;


  //  @Autowired
  //    private DiscoveryClient discoveryClient;//访问Eureka客户端

//    @GetMapping("{id}")
//    @HystrixCommand(fallbackMethod = "queryByIdFallBack")
//    public User queryById(@PathVariable("id") Long id) {

        //熔断问题
//        if (id == 1) {
//            throw new RuntimeException("服务忙！！");
//        }

//        //根据服务id,获取服务实例列表
//           List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//        //取出一个服务实例
//       ServiceInstance instance = instances.get(0);
//        //从实例中获取host和port,组成url
//        String host = instance.getHost();
//        int port = instance.getPort();
//        //发起web请求，并将json数据转换为User实例对象
//        User user = restTemplate.getForObject("http://" + host + ":" + port + "/user/" + id, User.class);
//        return user;

        //resTemplate访问Eureka客户端中的user-service爆404
        //通过底层ribbon获取Eureka中的所有服务实例
        //负载均衡获取一个具体服务实例，并且获取ip和端口

        //   return   restTemplate.getForObject("http://user-service/user/"+id,String.class);
        //    }

//    public String queryByIdFallBack(Long id){
//        return id+"抱歉，服务正忙！";
//    }
//    }
//  }
